package com.game;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

public class GameSelect extends Activity implements OnClickListener {
	private DBAdapter myDBAdapter;
	private Button PlayButton;
	private TextView gameNameEditText;
	private TextView gameDescTextView;
	private TextView gameInstrTextView;
	private int gameId;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectgame);
        
        PlayButton = (Button)findViewById(R.id.sg_playButton);
        PlayButton.setOnClickListener(this);
        
        gameId = -1;
        
		gameNameEditText = (TextView)findViewById(R.id.sg_gameTextViewSelect);
		gameNameEditText.setOnClickListener(this);
		gameDescTextView = (TextView)findViewById(R.id.sg_gameDescTextViewContent);
		gameInstrTextView = (TextView)findViewById(R.id.sg_gameInstrTextViewContent);
		
		myDBAdapter = ((txtGameApplication)this.getApplication()).getDatabaseAdapter();

    }
    
    public void onClick(View v){
    	switch (v.getId()){
    	case R.id.sg_playButton:
    		playGame(gameId);
    		break;
    	case R.id.sg_gameTextViewSelect:
    		viewGameName();
    		break;
    	}
    }
    
    
    private void playGame(int gameId){
    	
    	if(gameId != -1){
        Intent intent = new Intent(this, PlayGame.class);
        intent.putExtra("EXTRA_GAME_ID",gameId);
        startActivity(intent);
    	}
    	else{
    		viewGameName();
    	}
    
    }
    
    private void viewGameName(){
    	startActivityForResult(new Intent(this,ListGameActivity.class),0);
    }
    
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{  
		super.onActivityResult(requestCode, resultCode, intent);
		switch(requestCode)
		{
		case 0:
			
			if (resultCode == RESULT_OK && intent.hasExtra("ItemId"))
			{
			int _id = intent.getIntExtra("ItemId", -1);
			Log.v("GameSelect",Integer.toString(_id));
			Cursor cursor = myDBAdapter.getGameByIdCursor(_id);
			cursor.moveToFirst();
			gameId = cursor.getInt(cursor.getColumnIndex("_id"));
			gameDescTextView.setText(cursor.getString(cursor.getColumnIndex("game_desc")));
			gameInstrTextView.setText(cursor.getString(cursor.getColumnIndex("game_instr")));
			}
			break;
		}
	}
}
